import grace from "../../utils/grace.js"
grace.page({
      data: {
            address_id: 0,
            address: {
                  province: '广东省',
                  city: '珠海市',
                  district: '香洲区',
                  address: '',
                  customer_name: '',
                  customer_mobile: ''
            },
            defaultRegion: ['广东省', '珠海市', '香洲区']
      },
      onLoad: function(options) {
            if (options.length > 0) {
                  let params = options[0];
                  if (params.length > 0 && parseInt(params[0]['address_id']) > 0) {
                        this.$data.address_id = params[0]['address_id'];
                        this.loadData();
                  }
            }
            wx.hideNavigationBarLoading();
      },
      loadData: function() {
            this.$http.get('api/user_address/detail', {
                  address_id: this.$data.address_id
            }).then((res) => {
                  this.log(res);
                  if (res) {
                        this.$data.address = res;
                  }
            });
      },
      doSubmit: function() {
            let params = this.$data.address;
            let that = this;
            this.$http.post('api/user_address/add_or_update', params).then((res) => {
                  this.toast('操作成功');
                  this.$bus.$emit("update_addres_event");
                  that.$goBack({
                        is_update: true
                  });
            });
      },
      bindRegionChange: function(e) {
            let result = e.detail.value;
            this.$data.address.province = e.detail.value[0];
            this.$data.address.city = e.detail.value[1];
            this.$data.address.district = e.detail.value[2];

            this.$data.defaultRegion = e.detail.value;
      },
      setDefalut: function(e) {
            this.$data.address.is_default = this.$data.address.is_default == 1 ? 0 : 1;
      },
      inputHandle: function(e) {
            let name = e.target.dataset.name;
            this.$data.address[name] = e.detail.value;
      }
})